package com.yanh.merchant.service.impl;

import com.yanh.common.exception.BusinessException;
import com.yanh.common.util.QiniuUtils;
import com.yanh.merchant.service.IFileService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

/**
 * @author Administrator
 * @version 1.0
 **/
@Service
public class FileServiceImpl implements IFileService {

    private String qiniuUrl="";
    private String accessKey="4YtIfUb7poesjO43tpTp6u13imtNZPsE0JtiaxrY";
    private String secretKey="WSTXcDSVBnAKdwWQMJ7FvxpPJiwNRfRDc4mmgwD8";
    private String bucket="yanh";

    /**
     * 上传文件
     *
     * @param bytes    文件字节数组
     * @param fileName 文件名
     * @return 文件访问路径（绝对的url）
     * @throws BusinessException
     */
    @Override
    public String upload(byte[] bytes, String fileName) throws BusinessException {
        //调用common 下的工具类
        //String accessKey,String secretKey,String bucket, byte[] bytes,String fileName
        try {
            QiniuUtils.upload2qiniu(accessKey,secretKey,bucket,bytes,fileName);
        } catch (RuntimeException e) {
            e.printStackTrace();
            throw new BusinessException("上传失败");
        }
        //上传成功返回文件的访问地址（绝对路径）
        return qiniuUrl+fileName;
    }
}